/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.pro.addon;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import io.jboot.components.mq.JbootmqMessageListener;
import io.jboot.utils.QuietlyUtil;
import io.jpress.core.addon.AddonInfo;
import io.jpress.core.addon.AddonManager;
import io.jpress.core.addon.AddonUtil;

import java.io.File;
import java.io.IOException;

/**
 * @author michael yang (fuhai999@gmail.com)
 * @Date: 2020/3/1
 */
public class AddonMqListener implements JbootmqMessageListener {

    private AddonManager addonManager = AddonManager.me();
    private String mqClientId;

    public AddonMqListener(String mqClientId) {
        this.mqClientId = mqClientId;
    }

    @Override
    public void onMessage(String channel, Object message) {

        AddonMessage addonMessage = (AddonMessage) message;

        //不处理自己发给自己的消息
        if (addonMessage.getClientId().equals(mqClientId)) {
            return;
        }

        switch (addonMessage.getAction()) {
            case AddonMessage.ACTION_INSTALL:
                processAddonInstallByMessage(addonMessage.getPath());
                break;
            case AddonMessage.ACTION_START:
                processAddonStartByMessage(addonMessage.getAddonId());
                break;
            case AddonMessage.ACTION_STOP:
                addonManager.stop(addonMessage.getAddonId(), false);
                break;
            case AddonMessage.ACTION_UNINSTALL:
                addonManager.uninstall(addonMessage.getAddonId(), false);
                break;
        }
    }


    private void processAddonInstallByMessage(String path) {

        File jarFile = new File(PathKit.getWebRootPath(), path);


        AddonInfo addonInfo = AddonUtil.readAddonInfo(jarFile);

        //已经安装过
        if (addonManager.getAddonInfoMap().containsKey(addonInfo.getId())) {
            return;
        }

        addonManager.getAddonInfoMap().put(addonInfo.getId(), addonInfo);
        try {
            AddonUtil.unzipResources(addonInfo);
        } catch (IOException e) {
            LogKit.error(e.toString(), e);
        }

        //不需要再执行 addon.onInstall() 否则可能会出现数据库重复安装等问题
        //Addon addon = addonInfo.getAddon();
        //addon.onInstall(addonInfo);

        addonInfo.setStatus(AddonInfo.STATUS_INSTALL);
    }


    private void processAddonStartByMessage(String addonId) {
        int tryCount = 0;
        while (!addonManager.getAddonInfoMap().containsKey(addonId) && tryCount < 10) {
            tryCount++;
            QuietlyUtil.sleepQuietly(100);
        }

        if (addonManager.getAddonInfoMap().containsKey(addonId)) {
            addonManager.start(addonId, false);
        }
    }


}
